<template>
	<view class="page">
		<custom-header :title="title" @addListItem="addListItem" :type="APPTYPE.ADD" />
		<!-- #ifdef APP-PLUS -->
		<view style="height: 40px;"></view>
		<!-- #endif -->
		<!-- <u-sticky bgColor="#fff" class="sticky" :customNavHeight="height" zIndex="9">
			<view style="height: 10rpx;"></view>
		</u-sticky> -->
		<view class="list-panel">
			<template v-if="contacts.length > 0">
				<view class="item" v-for="(item, index) in contacts" :key="index" @click="toDetail(item, index)">
					<view class="info">
						<view class="name">联系人：{{ item.lxr }}</view>
						<view class="phone">联系信息：{{ item.lxfs }}</view>
					</view>
					<view class="edit">
						<u-icon name="arrow-right" color="#0003"></u-icon>
					</view>
				</view>
			</template>
			<u-empty v-else mode="data" icon="/static/empty/data.png"></u-empty>
		</view>
		<view style="height: 100rpx;"></view>
		<view class="space-x-1 px-1 btn_position">
			<!-- <u-button :disabled="isSubmit" type="primary" :plain="true" text="保存" @click="onDraft"></u-button> -->
			<u-button :disabled="isSubmit" type="primary" text="保存" @click="onSubmit"></u-button>
		</view>
		<custom-popup :pageTitle="pageTitle" :showPopup="showPopup" @close="close">
			<view class="approval">
				<view class="list-panel">
					<u--form :model="editItem" :rules="rules" ref="form" labelPosition="top" labelWidth="auto" :labelStyle="{
						fontSize: '16px',
						color: 'rgba(0,0,0,0.9)',
						lineHight: '20px'
					}">
						<u-form-item label="联系人" prop="lxr" borderBottom required>
							<u--input placeholder="请输入联系人" v-model="editItem.lxr" border="none" color="rgba(0, 0, 0, 0.5)"
								fontSize="16px" />
						</u-form-item>
						<u-form-item label="联系电话" prop="lxfs" required>
							<u--input type="number" placeholder="请输入联系电话" v-model="editItem.lxfs" border="none"
								color="rgba(0, 0, 0, 0.5)" fontSize="16px" />
						</u-form-item>
					</u--form>
				</view>
				<view class="approval_buttons">
					<view class="btn1" @click="onDel" v-if="curStatu === APPTYPE.EDIT">删 除</view>
					<view class="btn2" @click="onConfirm">保 存</view>
				</view>
			</view>
		</custom-popup>
	</view>
</template>

<script>
// import CustomHeader from '@/pages/purchase/components/customHeader.vue';
import APPTYPE from '@/config/appType.js';
export default {
	props: {},
	// components: { CustomHeader },
	data() {
		return {
			APPTYPE: APPTYPE,
			contacts: [],
			isSubmit: false,
			formData: {},
			height: 37,
			title: '联系人信息',
			pageTitle: '新增联系人',
			showPopup: false,
			editItem: {
				lxr: '',
				lxfs: ''
			},
			curStatu: '',
			rules: {
				lxr: [
					{ required: true, message: '请输入联系人!', trigger: ['blur', 'change'] },
				],
				lxfs: [
					{ required: true, message: '请输入联系电话!', trigger: ['blur', 'change'] },
					{ pattern: /^1\d{10}$|^(0\d{2,3}-?|\(0\d{2,3}\))?[1-9]\d{4,7}(-\d{1,8})?$/, message: '请输入正确的电话号码!', trigger: ['blur', 'change'] },
				]
			},
			index: null
		}
	},
	watch: {
	},
	onLoad(options) {
		this.contacts = JSON.parse(decodeURIComponent(options.formData)) || [];
	},
	created() {
		uni.getSystemInfo({
			success(e) {
				uni.setStorageSync('getSystemInfo', e);
			}
		})
		let safeArea = uni.getStorageSync('getSystemInfo').safeArea.top
		this.height = safeArea + 37
	},
	methods: {
		reset() {

		},
		onSubmit() {
			this.isSubmit = true;
			uni.$emit(this.APPTYPE.CONTACTS, this.contacts);
			uni.navigateBack({
				delta: 1
			})
		},
		addListItem() {
			this.showPopup = true;
			this.curStatu = this.APPTYPE.ADD;
			this.pageTitle = '新增联系人'
			this.editItem = {
				lxr: '',
				lxfs: ''
			}
		},
		toDetail(item, index) {
			this.showPopup = true;
			this.editItem = JSON.parse(JSON.stringify(item));
			this.index = index;
			this.curStatu = this.APPTYPE.EDIT;
			this.pageTitle = '编辑联系人'
		},
		open() {

		},
		close() {
			this.showPopup = false
			this.index = null
			this.editItem = {}
		},
		validateForm() {
			return this.$refs.form.validate()
		},
		async onConfirm() {
			await this.$refs.form.validate()
			const idx = this.index,
				data = this.editItem;
			if (typeof idx === 'number') {
				this.$set(this.contacts, idx, data)
			} else {
				this.contacts.push(data)
			}
			this.close();
		},
		onDel() {
			const that = this
			uni.showModal({
				content: '确定删除吗？',
				success: (res) => {
					if (res.confirm) {
						that.contacts = that.contacts.filter((i) => i !== that.editItem)
						that.close()
					}
				},
			})
		},
		// 获取safeArea
		getSafeAreaTop() {
			var top = 44
			return top
		},
		// 获取safeArea
		getScorollHeight() {
			let height = uni.getStorageSync('getSystemInfo').screenHeight - (uni.getStorageSync('getSystemInfo')
				?.safeArea?.top || 0) - (uni.getStorageSync('getSystemInfo')?.statusBarHeight || 0) - 44 - 32
			return height
		},
	}
}
</script>

<style scoped lang="scss">
@import url("@/assets/inputStyle.scss");

// .sticky {
// 	padding: 20rpx;
// }

.list-panel {
	margin-top: 54px;
	padding: 20rpx 40rpx 40rpx;
	background: #fff;

	.item {
		padding: 40rpx 0;
		display: flex;
		flex-flow: row nowrap;
		align-items: center;
		justify-content: space-between;
		border-bottom: 2rpx solid #F2F2F2;

		.info {
			display: flex;
			flex-flow: column nowrap;
			font-size: 16px;
			font-weight: 400;

			.name {
				margin-bottom: 20rpx;
				color: #171717;
			}

			.phone {
				color: #7F7F7F;
			}

		}
	}

	.item:last-child {
		border-bottom: none;
	}
}

.approval {
	width: 100%;
	height: 100%;
	background-color: #fff;

	.list-panel {
		margin: 0;
	}

	.approval_buttons {
		position: fixed;
		bottom: 0;
		left: 0;
		right: 0;
		padding: 24rpx 36rpx;
		display: flex;
		border-top: 2rpx solid #F2F2F2;
		z-index: 0;
		font-size: 17px;
		font-weight: 400;

		view {
			// width: 328rpx;
			width: 100%;
			height: 80rpx;
			border-radius: 5px;
			line-height: 80rpx;
			text-align: center;
		}

		.btn1 {
			width: 168rpx;
			background: rgba(0, 0, 0, 0.05);
			color: rgba(0, 0, 0, 0.5);
			margin-right: 24rpx;
		}

		.btn2 {
			background: #1E6FFF;
			color: #FFFFFF;
		}
	}
}
</style>